﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Licenta_Stomatologie.Panels
{
    public partial class AppointmentMainPanel : UserControl
    {
        private String[] appType = new String[] {"Control", "Interventie medicala" };
        private String[] achitat = new String[] { "Neachitat", "Achitat" };

        public AppointmentMainPanel()
        {
            InitializeComponent();
        }

        private void calDate_DateSelected(object sender, DateRangeEventArgs e)
        {
            ProjectDBEntities db = new ProjectDBEntities();

            DataTable dt = new DataTable("mainapp");

            DataColumn[] cols = new DataColumn[] { new DataColumn("id", typeof(int)), new DataColumn("idPacient", typeof(int)), new DataColumn("Pacient", typeof(String)), new DataColumn("Data", typeof(String)), new DataColumn("Ora inceperii", typeof(String)) ,new DataColumn("Durata", typeof(String)), new DataColumn("Tip", typeof(String)), new DataColumn("Suma de incasat", typeof(int)), new DataColumn("Achitat",typeof(String)) };

            dt.Columns.AddRange(cols);

            DateTime selDate = e.Start.Date;

            //MessageBox.Show( String.Format("Selected : {0} {1} {2}",selDate.Day, selDate.Month, selDate.Year) );

            IEnumerable<Appointment> apps = db.Appointments.Where(a => a.date.Value.Year == selDate.Year && a.date.Value.Month == selDate.Month && a.date.Value.Day == selDate.Day);

            foreach (Appointment a in apps)
            {
                Pacient pac = db.Pacients.First(p => p.id == a.pacientId);

                dt.Rows.Add(a.id, a.pacientId ,pac.nume + " " + pac.prenume, a.date.Value.ToShortDateString(), a.date.Value.ToShortTimeString() ,a.duration + " minute", appType[(int)a.type], a.cost, achitat[(int)a.achitat]);
            }

            dgvAppointments.DataSource = dt;

            dgvAppointments.Columns["id"].Visible = false;
            dgvAppointments.Columns["idPacient"].Visible = false;
        }

        private void dgvAppointments_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int pacientId = (int)dgvAppointments.SelectedRows[0].Cells["idPacient"].Value;

            this.Controls.Clear();
            this.Controls.Add(new Panels.AppointmentEditPanel(1));
        }

        private void dgvAppointments_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int pacientId = (int) dgvAppointments.SelectedRows[0].Cells["idPacient"].Value;
            int appId = (int)dgvAppointments.SelectedRows[0].Cells["id"].Value;

            this.Controls.Clear();
            this.Controls.Add(new Panels.AppointmentEditPanel(appId));
        }
    }
}
